package com.yc.jpyy.evaluation.controller;

import com.common.utils.manydatasource.HandlerDataSource;
import com.yc.jpyy.base.bean.RestResponse;
import com.yc.jpyy.base.controller.BaseController;
import com.yc.jpyy.base.util.PageBean;
import com.yc.jpyy.evaluation.service.EvaluationService;
import com.yc.jpyy.utils.Tools;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;
import java.util.Optional;

@Controller
@RequestMapping(value = "/evaluation")
@Scope("prototype")
public class EvaluationController extends BaseController {

    private static Logger logger = LoggerFactory.getLogger(EvaluationController.class);

    @Autowired
    private EvaluationService evaluationService;

    /**
     * 新增评价信息
     * @param info 投诉信息内容 json字符串
     * @return
     */
    @RequestMapping(value = "/addEvaluationForWx", method = { RequestMethod.GET,
            RequestMethod.POST }, produces = "application/json; charset=utf-8")
    @ResponseBody
    public String addEvaluationForWx(@RequestParam(required = true) String info, HttpServletRequest request) {
        logger.info("新增微信评价内容:" + info);
        RestResponse response = new RestResponse(Optional.ofNullable(request.getAttribute("pubKey")).orElse(""));
        try{
            //设置数据源
            Map<String,Object> paramsMap = Tools.JsonToMap(info);
            logger.info("paramsMap = " + paramsMap);
            String dbcode = paramsMap.get("dbcode").toString();
            HandlerDataSource.setDataSource(dbcode);
            evaluationService.addEvaluationForWxService(paramsMap);
            logger.info("新增评价成功");
            response.setReturnMsg(1, "操作成功");
        }catch (Exception e){
            e.printStackTrace();
            logger.info(e.getMessage());
            response.setReturnMsg(0,"添加失败");
            response.setReturnException(e.toString());
            if(e.getMessage().trim().equals("此用户为黑名单用户")) {
            	response.setReturnMsg(0,"此用户为黑名单用户");
            }
        }finally {
            HandlerDataSource.clear();
        }
        return response.toJSON();
    }
    
    /**
     * 新增评价信息
     * @param info 投诉信息内容 json字符串
     * @return
     */
    @RequestMapping(value = "/add", method = { RequestMethod.GET,
            RequestMethod.POST }, produces = "application/json; charset=utf-8")
    @ResponseBody
    public String add(@RequestParam(required = true) String info, HttpServletRequest request) {
        logger.info("新增评价内容:" + info);
        RestResponse response = new RestResponse(Optional.ofNullable(request.getAttribute("pubKey")).orElse(""));
        try{
            //设置数据源
            Map<String,Object> paramsMap = Tools.JsonToMap(info);
            logger.info("paramsMap = " + paramsMap);
            String dbcode = paramsMap.get("dbcode").toString();
            HandlerDataSource.setDataSource(dbcode);
            evaluationService.add(paramsMap);
            logger.info("新增评价成功");
            response.setReturnMsg(1, "操作成功");
        }catch (Exception e){
            e.printStackTrace();
            logger.info(e.getMessage());
            response.setReturnMsg(0,"添加失败");
            response.setReturnException(e.toString());
        }finally {
            HandlerDataSource.clear();
        }
        return response.toJSON();
    }


    /**
     * 根据id查找单条记录
     * @param info
     * @return
     */
    @SuppressWarnings("finally")
    @RequestMapping(value = "/findById", method = { RequestMethod.GET,
            RequestMethod.POST }, produces = "application/json; charset=utf-8")
    @ResponseBody
    public String findById(@RequestParam(required = true) String info,HttpServletRequest request){
        RestResponse response = new RestResponse(Optional.ofNullable(request.getAttribute("pubKey")).orElse(""));
        try {
            Map<String,Object> paramsMap = Tools.JsonToMap(info);
            String dbcode = paramsMap.get("dbcode").toString();
            //设置数据源
            HandlerDataSource.setDataSource(dbcode);
            Map<String, Object> val = this.evaluationService.findById(paramsMap);
            int retSize = val.size();
            if(retSize==0) {
                response.setReturnMsg(1, "无查询数据");
                return response.toJSON();
            }
            else {
                response.setReturnMsg(1, "查询单条数据成功");
                response.setListData(val);
                return response.toJSON();
            }

        } catch (Exception e) {
            logger.info("查询单条数据异常" + e.toString());
            // 异常信息
            response.setReturnCode(0);
            response.setReturnMsg("查询单条数据异常");
        } finally {
            HandlerDataSource.clear();
            return response.toJSON();
        }
    }


    /**
     * 分页查询
     * @param info
     * @return
     */
    @SuppressWarnings("finally")
    @RequestMapping(value = "/findPage", method = { RequestMethod.GET,
            RequestMethod.POST }, produces = "application/json; charset=utf-8")
    @ResponseBody
    public String findPage(@RequestParam(required = false) String info,HttpServletRequest request){
        RestResponse response = new RestResponse(Optional.ofNullable(request.getAttribute("pubKey")).orElse(""));
        logger.info("info="+info);
        try {
            Map<String,Object> paramsMap = Tools.JsonToMap(info);
            logger.info("paramsMap = " + paramsMap);
            String dbcode = paramsMap.get("dbcode").toString();
            //设置数据源
            HandlerDataSource.setDataSource(dbcode);
            PageBean pageBean = this.evaluationService.findPage(paramsMap);
            response.setReturnMsg(1, "操作成功");
            response.setTotalCount(pageBean.getAllRow());
            response.setTotalPage(pageBean.getTotalPage());
            response.setListData(pageBean.getList());
            return response.toJSON();

        } catch (Exception e) {
            e.printStackTrace();
            logger.error("findPage" + e.getMessage());
            response.setReturnCode(1);
            response.setReturnMsg("查询失败");
            response.setReturnException(e.getMessage());
        } finally {
            HandlerDataSource.clear();
            return response.toJSON();
        }
    }
}
